package descentFly.away3D4.element.Extends
{
	import UIFree.ObjectEvent;
	import UIFree.uif_Timer;
	
	import descentFly.away3D4.df_timer;
	import descentFly.away3D4.element.df_element;
	
	import flash.events.Event;
	
	public class df_teleportController extends df_distanceController {
		public static const GOTOTELEPORT	: String = 'GOTOTELEPORT';
		public static const NEXTLEVEL		: int = 1; 
		public static const PREVLEVEL		: int = -1; 
		
		private var _blocking	: Boolean; 
		public function df_teleportController(a_ctrl:df_element, a_options:Object) {
			super(a_ctrl, a_options);
			_blocking = false;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			ctrl.Space.addEventListener(GOTOTELEPORT, doGotoPort);
		}
	
		override protected function removeEvents():void {
			super.removeEvents();
			ctrl.Space.removeEventListener(GOTOTELEPORT, doGotoPort);
		}
		
		protected function doGotoPort(e: ObjectEvent): void {
			if ((e.object != this) && 
				(e.object.options.group == options.group)) gotoMe();
		}
		
		protected function gotoMe(): void {
			_blocking = true;
			myChip.body.currentState.position = ctrl.getModel().position;
			nearState = true;
			_blocking = false;
		}
		
		override protected function doNearest():void {
			if (!_blocking) {
				super.doNearest();
				uif_Timer.one(500, function(e: Event): void {
					if (options.level) ctrl.Space.levelID = ctrl.Space.scene.id + options.level;
					else ctrl.Space.dispatchEvent(new ObjectEvent(GOTOTELEPORT, This));
				});
			}
		}
	}
}